import com.alibaba.fastjson.JSON;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * @Author: zhs
 * @Date: 2021/5/20 10:32
 * @Description:
 * 使用HttpClient发送请求
 */
public class HttpUtils {

    /**
     * 发送get请求
     * @param url 请求路径
     * @param urlParam url参数 ?id=1&name=2
     * @param headers 请求头
     * @return
     */
    public static String httpGet(String url, Map<String,Object> urlParam, Header... headers){
        //1.解析参数
        String param = HttpParamUtils.httpGetParam(urlParam);
        //2.创建client与response
        CloseableHttpClient client = HttpClientBuilder.create().build();
        CloseableHttpResponse response = null;
        //3.创建httpGet请求
        HttpGet httpGet = new HttpGet(url + param);
        //4.设置请求头
        httpGet.setHeaders(headers);
        try {
            //5.发送请求
            response  = client.execute(httpGet);
            //6.获得返回内容
            HttpEntity entity = response.getEntity();
            if(entity != null){
                return EntityUtils.toString(entity, "UTF-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(client != null){
                    client.close();
                }
                if(response != null){
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }


    /**
     * 发送post请求
     * @param url 请求路径
     * @param urlParam url参数 ?id=1&name=2
     * @param bodyParam 请求体参数
     * @param headers 请求头
     * @return
     */
    public static String httpPost(String url, Map<String,Object> urlParam ,Object bodyParam, Header... headers){
        //1.创建client与response
        CloseableHttpClient client = HttpClientBuilder.create().build();
        CloseableHttpResponse response = null;
        //2.创建httpPost请求
        if(urlParam != null){
            url = url + HttpParamUtils.httpGetParam(urlParam);
        }
        HttpPost httpPost = new HttpPost(url);
        //3.设置参数
        if(bodyParam != null){
            StringEntity entity = new StringEntity(JSON.toJSONString(bodyParam), "UTF-8");
            httpPost.setEntity(entity);
        }
        //4.设置请求头
        httpPost.setHeaders(headers);
        try {
            //5.发送请求
            response  = client.execute(httpPost);
            //6.获得返回内容
            HttpEntity entity = response.getEntity();
            if(entity != null){
                return EntityUtils.toString(entity, "UTF-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(client != null){
                    client.close();
                }
                if(response != null){
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }


    /**
     * 以post请求的形式发送byte[] 文件
     * @param url 请求路径
     * @param file 发送的文件
     * @param urlParam url参数 ?id=1&name=2
     * @param bodyParam 请求体参数
     * @param headers 请求头
     * @return
     */
    public static String httpPostBinaryFile(String url, File file , Map<String,Object> urlParam ,Object bodyParam, Header... headers){
        //1.创建client与response
        CloseableHttpClient client = HttpClientBuilder.create().build();
        CloseableHttpResponse response = null;
        //2.创建httpPost请求
        if(urlParam != null){
            url = url + HttpParamUtils.httpGetParam(urlParam);
        }
        HttpPost httpPost = new HttpPost(url);
        //3.设置参数
        if(file == null){
            throw new NullPointerException();
        }else{
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            builder.addBinaryBody("video",file, ContentType.MULTIPART_FORM_DATA,file.getName());
            httpPost.setEntity(builder.build());
        }
        if(bodyParam != null){
            StringEntity entity = new StringEntity(JSON.toJSONString(bodyParam), "UTF-8");
            httpPost.setEntity(entity);
        }
        //4.设置请求头
        httpPost.setHeaders(headers);
        try {
            //5.发送请求
            response  = client.execute(httpPost);
            //6.获得返回内容
            HttpEntity entity = response.getEntity();
            if(entity != null){
                return EntityUtils.toString(entity, "UTF-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(client != null){
                    client.close();
                }
                if(response != null){
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "";
    }

}